<template>
  <!-- 通栏 -->
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap topwrap">
      <!-- 左标题 -->
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <!-- 右菜单栏 -->
      <ul class="navs">
        <li>
          <img :src="userInfo.headImg" alt="" class="imgs" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="active" v-else @click="changeIsShowModal(true)">登录</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { wechatUsersAPI } from "@/request/api.js";
export default {
  methods: {
    ...mapMutations({
      // 登录框
      changeIsShowModal: "ShowModal/changeIsShowModal",
      // 登录状态
      changeIsShowLogin: "LoginState/changeIsShowLogin",
      // 初始用户信息
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncToast: "ShowToast/asyncToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.LoginState.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },

  created() {
    // 等 router 初始化完毕再获取 code
    this.$router.onReady(async () => {
      let pathCode = this.$route.query.code;
      // console.log("code", pathCode);

      if (pathCode) {
        // 发起微信登录请求
        let req = await wechatUsersAPI({
          code: pathCode,
        });
        // console.log("****req***", req);
        // code 0
        // console.log('-------------------',req.code);
        if (req.code == 0) {
          // a.提示成功
          this.asyncToast({
            showToast: true,
            text: "登录成功!!",
            type: "success",
          });
          // b.存储token
          localStorage.setItem("x-auth-token", req["x-auth-token"]);
          // c.更新登录状态
          this.changeIsShowLogin(true);
          // d.清除code 自动刷新
          this.$router.push(this.$route.path);

          // 更新用户信息
          this.asyncChangeUserInfo();
        }
        // code 400
        else if (req.code == 400) {
          // 1.提示重新扫码
          this.asyncToast({
            showToast: true,
            text: "请重新扫码！",
            type: "danger",
          });
          // 2.自动打开登录框
          this.changeIsShowModal(true);
        }
        // code 407 第一次微信登录
        else if (req.code == 407) {
          // 1.提示需再进行一次手机绑定
          this.asyncToast({
            showToast: true,
            text: "首次微信扫码需绑定手机号码",
            type: "wran",
          });
          // 2.存储uuid
          localStorage.setItem("uuid", req.uuid);
          // 3.自动打开登录框
          this.changeIsShowModal(true);
          // 4.清除地址栏code
          this.$router.push(this.$route.path);
        }
      } else {
        console.log("普通的页面切换");
        // 重载后 判断 token 更新 登录状态
        let token = localStorage.getItem("x-auth-token");
        this.changeIsShowLogin(Boolean(token));

        // 显示用户信息
        if (token) {
          this.asyncChangeUserInfo();
        } else {
          this.initUserInfo();
        }
      }
    });
  },
  // watch: {
  //   // 手动删除 token ,改变登录状态
  //   // 方式一 watch 监听数据变化
  //   "$route.path": {
  //     handler() {
  //       let token = localStorage.getItem("x-auth-token");
  //       // if (token) {
  //       //   this.changeIsShowLogin(true);
  //       // } else {
  //       //   this.changeIsShowLogin(false);
  //       // }
  //       this.changeIsShowLogin(Boolean(token)); //优化
  //     },
  //   },
  // 方式二：组件重载
  // 1.给topbar增加一个key属性 topbarkey
  //  2.监听地址变化，更新key值
  // },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  background: #242b39;
  .topwrap {
    font-size: 14px;
    font-weight: 300;
    color: #fffefe;
    display: flex;
    justify-content: space-between;
    line-height: 40px;
  }
  .navs {
    display: flex;
    li {
      margin-left: 20px;
      cursor: pointer;
      text-align: center;
      .imgs {
        width: 23px;
        height: 22px;
        border-radius: 13px;
        vertical-align: middle;
      }
      &.active {
        width: 124px;
        height: 40px;
        background: #0a328e;
      }
      &.cart-btn {
        width: 124px;
        height: 40px;
        background: #0a328e;
        text-align: center;
        img {
          width: 20px;
          height: 19px;
          vertical-align: middle;
        }
        span {
          margin: 0 8px;
        }
        strong {
          width: 22px;
          height: 22px;
          line-height: 22px;
          display: inline-block;
          border-radius: 50%;
          background-color: #fd604d;
        }
      }
    }
  }
}
</style>